#include "types.h"
#include "stat.h"
#include "user.h"

char buf[512];

int
main(int argc, char *argv[])
{
  int pid[3];
  pid[0]=fork();
  if(pid[0]!=0)
  {
    pid[1]=fork();
    if(pid[1]!=0)
    {
      pid[2]=fork();
      if(pid[2]!=0)
      {
	int counter =0;
	while(1)
	{
	  write(1,"Enter a child id (0 - 2): ",sizeof("Enter a child id (0 - 2): "));
	  read(1,buf,sizeof(buf));
	  int childNum=atoi(buf);
	  if(!(childNum>=0 && childNum<=2)){
	      write(1,"Wrong Input\n",sizeof("Wrong Input\n"));
	      continue;
	  }
	  while(1){
	    write(1,"Which signal to send: ",sizeof("Which signal to send: "));
	    read(1,buf,sizeof(buf));
	    int sigNum = atoi(buf);
	    if(!(sigNum>=0 && sigNum<=4)){
	      write(1,"Wrong Input\n",sizeof("Wrong Input\n"));
	      continue;
	    }
	    if(sigsend(pid[childNum],sigNum)!=-1){
	      if(sigNum==0){
		if(wait()!=-1)
		  counter++;
		if(counter==3)
		  exit();
	      }
	    }
	    else{
	      printf(1,"Procces already dead!\n");
	    }
	    sleep(50);
	    break;
	  }
	}
      }
      //pid[2] code
      else{
	while(1){
	  sleep(50);
	}
      }
    }
    //pid[1] code
    else{
      while(1){
	sleep(50);
      }
     }
  }
  //pid[0] code
  else{
    while(1){
      sleep(50);
    }
  }
  exit();
}
